;
(function (window, angular) {
    'use strict';
    angular.module("Controller.Report.AgentPersonAnaly", [])
        .controller("AgentPersonAnalyCtrl", ["$scope", "ybWidget", "ybHttp", "MESG_TYPE", "UserContextService", "$timeout", "$filter", function ($scope, ybWidget, ybHttp, MESG_TYPE, UserContextService, $timeout, $filter) {
            // 高级搜索的展示与隐藏
            $scope.toolbarConfig = {
                showAgentAdSearch: false,
            };
            // 切换高级搜索展示与隐藏
            $scope.switchToolbar = function (toolbar) {
                if (!!$scope.toolbarConfig[toolbar]) {
                    $scope.toolbarConfig[toolbar] = false;
                } else {
                    $timeout(function () {
                        $scope.toolbarConfig[toolbar] = true;
                    });
                }
            };

            $scope.exportParam = {
                allUrl: ybHttp.RequestUrl("device", "/employeeSetting/exportRpt")
            };


            // 列表查询条件
            $scope.condition = {};
            // 表格信息
            $scope.tableConfig = {};

            $scope.markInfo = {
                curRow: -1
            };
            // 分页信息
            $scope.pageInfo = {
                totalPages: 1,
                totalRows: 0,
                pageSize: 10,
                curPage: 1
            };
            // 人员列表信息
            $scope.dataList = [];

            // 当前选中行
            $scope.selectRow = function (index) {
                $scope.markInfo.curRow = index
            };

            // 查询人员列表
            $scope.search = function (curPage) {
                if (curPage) $scope.pageInfo.curPage = curPage;
                getColumnsAndData();
            };

            function getColumnsAndData() {
                var url = "/employeeSetting/list";
                var param = {
                    filters: {
                        companyNameLikeList: $scope.condition.companyNameLike ? [$scope.condition.companyNameLike] : [],
                        jobIdList: $scope.condition.jobIdList || []
                    },
                    pageSize: $scope.pageInfo.pageSize,
                    pageNum: $scope.pageInfo.curPage,
                };
                ybHttp.Post('device', url, param).then(function (res) {
                    if (res.code !== 0) {
                        ybWidget.ShowMessage(res, MESG_TYPE.Error);
                        return;
                    }
                    if (!res.value) return;
                    setHeader(res.value.jobs);
                    setPersonList(res.value.employeeSettingVOs);
                });
            }

            function setHeader(headers) {
                var mockRes = [
                    {
                        name: "小计",
                        childs: [
                            { name: "标准", value: "totalSizeNumberEmp" },
                            { name: "配置", value: 'totalSizeNumberEmpReal' },
                            { name: "缺编", value: 'totalSizeNumberEmpNeed' },
                            { name: "配置率", value: 'totalSizeConfigRate' },
                        ]
                    }
                ];
                angular.forEach(headers, function (item) {
                    mockRes.push({
                        name: item.name,
                        childs: [
                            { name: "标准", value: item.id + "NumberEmp" },
                            { name: "配置", value: item.id + "NumberEmpReal" },
                            { name: "缺编", value: item.id + "NumberEmpNeed" },
                            { name: "配置率", value: item.id + "ConfigRate" },
                        ]
                    });
                })
                var firstTableCol = [];
                var secondTableCol = [];
                var tdWidth = 70; // 表格列宽度
                angular.forEach(mockRes, function (item) {
                    if (item.childs.length > 0) {
                        var isShowNum = 0;
                        angular.forEach(item.childs, function (second) {
                            isShowNum++;
                            secondTableCol.push({
                                name: second.name,
                                width: tdWidth,
                                dataField: second.value
                            });
                        });
                        firstTableCol.push({
                            name: item.name,
                            width: tdWidth * isShowNum,
                            colSpan: isShowNum
                        });
                    }
                });
                $scope.tableConfig.firstTableCol = firstTableCol;
                $scope.tableConfig.secondTableCol = secondTableCol;
                $scope.tableConfig.fixedWidth = 60 + 140; // 序号和代理商列宽度
                $scope.tableConfig.scrollWidth = $scope.tableConfig.fixedWidth + tdWidth * secondTableCol.length;
                getJobs(headers);
            }

            function setPersonList(value) {
                var persons = [];
                angular.forEach(value.list, function (agentItems) {
                    var tempObj = {};
                    var totalNumberEmp = 0;
                    var totalNumberEmpReal = 0;
                    var totalNumberEmpNeed = 0;
                    angular.forEach(agentItems, function (personItem) {
                        var numberEmpVal = parseInt(personItem.numberEmp) || 0;
                        var numberEmpRealVal = parseInt(personItem.numberEmpReal) || 0;
                        var numberEmpNeedVal = parseInt(personItem.numberEmpNeed) || 0;
                        tempObj[personItem.jobId + "NumberEmp"] = numberEmpVal;
                        tempObj[personItem.jobId + "NumberEmpReal"] = numberEmpRealVal;
                        tempObj[personItem.jobId + "NumberEmpNeed"] = numberEmpNeedVal;
                        tempObj[personItem.jobId + "ConfigRate"] = numberEmpVal ? (numberEmpRealVal / numberEmpVal * 100).toFixed(2) + "%" : "0.00%";
                        tempObj.orgName = personItem.orgName;
                        totalNumberEmp += numberEmpVal;
                        totalNumberEmpReal += numberEmpRealVal;
                        totalNumberEmpNeed += numberEmpNeedVal;
                    });
                    tempObj.totalSizeNumberEmp = totalNumberEmp;
                    tempObj.totalSizeNumberEmpReal = totalNumberEmpReal;
                    tempObj.totalSizeNumberEmpNeed = totalNumberEmpNeed;
                    tempObj.totalSizeConfigRate = totalNumberEmp ? (totalNumberEmpReal / totalNumberEmp * 100).toFixed(2) + "%" : "0.00%";
                    persons.push(tempObj);
                });
                $scope.pageInfo.totalRows = value.total;
                $scope.pageInfo.totalPages = value.pages;
                $scope.dataList = persons;
            }

            function getJobs(headers) {
                $scope.jobList = headers;
            }

            $scope.selectJob = function () {
                var checkedJobNames = []; // 已选中的岗位name
                var checkedJobIds = []; // 已选中的岗位id
                angular.forEach($scope.jobList, function (item) {
                    if (item.isChecked) {
                        checkedJobNames.push(item.name);
                        checkedJobIds.push(item.id);
                    }
                });
                $scope.jobNames = checkedJobNames.join(',');
                $scope.condition.jobIdList = checkedJobIds;
            };

            $scope.reset = function () {
                $scope.jobNames = "";
                $scope.condition.jobIdList = [];
                $scope.condition.companyNameLike = "";
            };

            var btnPermissionCode = {
                agentPersonConfigExport: window.ActionCode.agentPersonConfigExport,
            };
            $scope.btnPermission = {
                agentPersonConfigExport: true
            };
            function setBtnPermission() {
                for (var key in btnPermissionCode) {
                    $scope.btnPermission[key] = UserContextService.CheckPermission([btnPermissionCode[key]]);
                }
            }

            // 初始化页面信息
            (function () {
                UserContextService.SetBtnPermission(setBtnPermission);
                getColumnsAndData();
            })()
        }])
        ;
})(window, window.angular);